package com.wmh.baseservice.common.wechat.ma.template;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.utils.CommUtil;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * 订阅消息工具类
 */
@Component
public class WxTemplateMsgUtils {

    @Resource
    private WxMaService wxMaService;

    public static WxMaService staticWxMaService;

    @PostConstruct
    public void init() {
        staticWxMaService = wxMaService;
    }



    /**
     * 商家入驻通知
     *
     * @param openId 接收方openId
     * @return true 发送成功 false 发送失败
     */
    public static boolean miniSendRegister(String openId, JSONObject object) {
        ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData = new ArrayList<>();
        WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
        data.setName("phrase1");
        data.setValue(object.getString("phrase1"));
        wxMaSubscribeData.add(data);
        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("thing2");
        data1.setValue(object.getString("thing2"));
        wxMaSubscribeData.add(data1);
        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing4");
        data2.setValue(object.getString("thing4"));
        wxMaSubscribeData.add(data2);
        return sendTemplate(openId, wxMaSubscribeData, "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_REGISTER);
    }

    /**
     * 通知用户退款进度通知
     *
     * @param openId 接收方openId
     * @return true 发送成功 false 发送失败
     */
    public static boolean noticeUserRefund(String openId, JSONObject object) {
        ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData = new ArrayList<>();
        WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
        data.setName("character_string1");
        data.setValue(object.getString("character_string1"));
        wxMaSubscribeData.add(data);
        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("time2");
        data1.setValue(object.getString("time2"));
        wxMaSubscribeData.add(data1);
        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing3");
        data2.setValue(object.getString("thing3"));
        wxMaSubscribeData.add(data2);
        WxMaSubscribeMessage.Data data3 = new WxMaSubscribeMessage.Data();
        data3.setName("thing4");
        data3.setValue(object.getString("thing4"));
        wxMaSubscribeData.add(data3);
        return sendTemplate(openId, wxMaSubscribeData, "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_NOTICE_USER_REFUND);
    }

    /**
     * 通知用户发货
     *
     * @param openId 接收方openId
     * @return true 发送成功 false 发送失败
     */
    public static boolean noticeUserDelivery(String openId, JSONObject object) {
        ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData = new ArrayList<>();
        WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
        data.setName("thing2");
        data.setValue(object.getString("thing2"));
        wxMaSubscribeData.add(data);
        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("name3");
        data1.setValue(object.getString("name3"));
        wxMaSubscribeData.add(data1);
        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing4");
        data2.setValue(object.getString("thing4"));
        wxMaSubscribeData.add(data2);
        WxMaSubscribeMessage.Data data3 = new WxMaSubscribeMessage.Data();
        data3.setName("thing5");
        data3.setValue(object.getString("thing5"));
        wxMaSubscribeData.add(data3);
        WxMaSubscribeMessage.Data data4 = new WxMaSubscribeMessage.Data();
        data4.setName("thing12");
        data4.setValue(object.getString("thing12"));
        wxMaSubscribeData.add(data4);
        return sendTemplate(openId, wxMaSubscribeData, "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_NOTICE_USER_DELIVERY);
    }

    /**
     * 采摘订单通知用户自提
     *
     * @param openId 接收方openId
     * @return true 发送成功 false 发送失败
     */
    public static boolean noticeUserPickGoods(String openId, JSONObject object) {
        ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData = new ArrayList<>();
        WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
        data.setName("thing2");
        data.setValue(object.getString("thing2"));
        wxMaSubscribeData.add(data);
        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("thing3");
        data1.setValue(object.getString("thing3"));
        wxMaSubscribeData.add(data1);
        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing4");
        data2.setValue(object.getString("thing4"));
        wxMaSubscribeData.add(data2);
        WxMaSubscribeMessage.Data data3 = new WxMaSubscribeMessage.Data();
        data3.setName("phone_number10");
        data3.setValue(object.getString("phone_number10"));
        wxMaSubscribeData.add(data3);
        return sendTemplate(openId, wxMaSubscribeData, "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_NOTICE_USER_PICK_GOODS);
    }

    /**
     * 租赁区域到期通知
     * @param openId 接收方openId
     * @return true 发送成功 false 发送失败
     */
    public static boolean rentNoticePickGoods(String openId, JSONObject object) {
        ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData = new ArrayList<>();
        WxMaSubscribeMessage.Data data = new WxMaSubscribeMessage.Data();
        data.setName("number3");
        data.setValue(object.getString("number3"));
        wxMaSubscribeData.add(data);
        WxMaSubscribeMessage.Data data1 = new WxMaSubscribeMessage.Data();
        data1.setName("thing4");
        data1.setValue(object.getString("thing4"));
        wxMaSubscribeData.add(data1);
        WxMaSubscribeMessage.Data data2 = new WxMaSubscribeMessage.Data();
        data2.setName("thing5");
        data2.setValue(object.getString("thing5"));
        wxMaSubscribeData.add(data2);
        return sendTemplate(openId, wxMaSubscribeData, "pages/growVegetables/growVegetables", WxTemplateMsgConstants.TEMP_NOTICE_REGION_RENT);
    }

    public static boolean sendTemplate(String openId, ArrayList<WxMaSubscribeMessage.Data> wxMaSubscribeData, String page, String templateId) {
        try {
            WxMaSubscribeMessage message = WxMaSubscribeMessage.builder()
                    // 要给谁发送
                    .toUser(openId)
                    // 模板id
                    .templateId(templateId)
                    // 数据
                    .data(wxMaSubscribeData)
                    .build();
            if (CommUtil.checkNull(page)) {
                message.setPage(page);
            }
            staticWxMaService.getMsgService().sendSubscribeMsg(message);
            return true;
        } catch (WxErrorException e) {
            e.printStackTrace();
            return false;
        }
    }
}
